package pl.elka.simulator.gui.swt.menu;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import pl.elka.simulator.core.Simulator;
import pl.elka.simulator.gui.swt.IReloadable;
import pl.elka.simulator.mediator.SimulationFasade;
import pl.elka.simulator.persistance.IPersister;
import pl.elka.simulator.persistance.xml.XMLPersister;

public class LoadSelection extends SelectionAdapter {
	private IPersister persister = new XMLPersister();
	
	private Shell shell;
	public LoadSelection(Shell shell){
		this.shell = shell;
	}
	
	@Override
	public void widgetSelected(SelectionEvent e) {
		FileDialog fd = new FileDialog(shell, SWT.OPEN);
		fd.setText("Wszytaj");
		fd.setFilterPath("C:/");
		String[] filterExt = { "*." + persister.getPersistanceType().getFileType()};
		fd.setFilterExtensions(filterExt);
		String selected = fd.open();	  
		if(selected!=null){
			Simulator s = persister.load(selected);
			SimulationFasade.getInstance().setSimulator(s);
		}
		shell.redraw();
		for(Control c :shell.getChildren()){
			if(c instanceof IReloadable){
				((IReloadable)c).reloadContent();
			}
		}
	}
}
